home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / c80tcog.lbr / QSORT.C < prev    next >
Text File  |  1985-08-09  |  896b  |  37 lines

  1. /*
  2.     Contributed by Les Johnson, 12/21/82
  3.  
  4.     This is the qsort routine, utilizing the 
  5.     shell sort technique given in the Software 
  6.     Tools book (by Kernighan & Plauger).
  7.  
  8.     For C/80 from The Software Toolworks.
  9. */
  10.  
  11. qsort(base, nel, width, compar)
  12. char *base; int (*compar)();
  13. unsigned width,nel;
  14. {    int i, j;
  15.     unsigned gap, ngap, t1;
  16.     int jd, t2;
  17.     t1 = nel * width;
  18.     for (ngap = nel / 2; ngap > 0; ngap /= 2) {
  19.        gap = ngap * width;
  20.        t2 = gap + width;
  21.        jd = base + gap;
  22.        for (i = t2; i <= t1; i += width)
  23.           for (j =  i - t2; j >= 0; j -= gap) {
  24.         if ((*compar)(base+j, jd+j) <=0) break;
  25.              _swp(width, base+j, jd+j);
  26.           }
  27.     }
  28. }
  29.  
  30. _swp(w,a,b) /* swap, used by qsort */
  31. char *a,*b;
  32. unsigned w;
  33. {
  34.     char tmp;
  35.     while(w--) {tmp = *a; *a++ = *b; *b++ = tmp;}
  36. }
  37.